<?php

namespace App\Http\Controllers\Test;

use App\Http\Controllers\Controller;
use App\Models\News;
use App\Models\NewsDesc;
use Illuminate\Support\Facades\Mail;


class MailController extends Controller
{
    // 发送邮件测试
    public function initialize()
    {
        parent::initialize();
    }

    // 展示邮件
    // http://www.atw.com/test/mail/email
    public function email()
    {
        // 获取时间：昨天的时间
        // 因为这几天没有数据，所以固定某一天的数据
        //$yesDay    = "2024-3-4";
        $yesDay    = date('Y-m-d', strtotime("-1 days"));
        $beginTime = strtotime($yesDay) - 86400 * 1000;
        $beginTime = strtotime($yesDay) - 86400;
        $endTime   = strtotime($yesDay) + 86400 - 1;

        // 今天
        //$toDay     = date('Y-m-d');
        //$beginTime = strtotime($toDay);
        //$endTime   = strtotime($toDay) + 86400 - 1;

        // 获取数据
        $where1 = [['ctime', '>=', $beginTime], ['ctime', '<=', $endTime], ['is_draft', '=', 0], ['verify', '=', 1], ['is_email', '=', 1], ['uid', '=', 0],];
        $where2 = [['ctime', '>=', $beginTime], ['ctime', '<=', $endTime], ['is_draft', '=', 0], ['verify', '=', 1], ['uid', '>', 0]];
        $lists1 = (new News())->getList($where1, 0);
        $lists2 = (new News())->getList($where2, 0);
        $lists  = array_merge($lists1, $lists2);

        // 文章详情 制作描述
        $descs = (new NewsDesc())->getListInIds(array_column($lists, 'id'), '*', 'news_id');
        foreach($descs as $_key => $_desc){
            $number = 0;
            if(strpos($_desc['en_content'], '<img') !== false){
                $number = 1;
            }

            // 截取第一段
            $en_contents = explode('</p>', $_desc['en_content']);
            $cn_contents = explode('</p>', $_desc['cn_content']);
            $tc_contents = explode('</p>', $_desc['tc_content']);

            // 重新组合
            $descs[$_key]['en_content'] = ($en_contents[$number] ?? $en_contents[0]).'</p>';
            $descs[$_key]['cn_content'] = ($cn_contents[$number] ?? $cn_contents[0]).'</p>';
            $descs[$_key]['tc_content'] = ($tc_contents[$number] ?? $tc_contents[0]).'</p>';
        }

        $nList     = [];
        $domain    = env('STYLE_HOST');
        $oldDomain = env('OLD_HOST');
        foreach($lists as $_row){
            $date  = date('Y.m.d', $_row['ctime']);
            $_desc = $descs[$_row['id']] ?? [];
            // 整理
            $_row['date']           = $date;
            $_row['pic']            = str_replace($oldDomain, $domain, $_row['pic']);
            $_row['en_description'] = isset($_desc['en_content']) ? strip_tags($_desc['en_content']) : '';
            $_row['cn_description'] = isset($_desc['cn_content']) ? strip_tags($_desc['cn_content']) : '';
            // 整理
            $nList[$date][] = $_row;
        }

        // 结果整理
        $result['nList']      = $nList;
        $result['style_host'] = env('STYLE_HOST');
        $result['app_host']   = request()->getSchemeAndHttpHost().'/';


        //return $this->dispaly('emails.atw_email', $result);
        return view('emails.atw_email', $result);
    }


    // 测试
    // https://slh.shenlannews.com/test/mail/send
    // http://www.atw.com/test/mail/send
    // 深蓝汇发送邮件
    // lisen1004@qq.com
    // SClzliuxu231590
    // 授权码（在qq邮箱中设置获取）
    // fekerjeqpfiobhga
    public function send()
    {
        $email = '23231590@qq.com';
        #$email = '914432635@qq.com';
        $code  = rand(1000, 9999);

        //使用blade模板发送(引用的模板)
        // Mail::send(模板文件, [参数], function($message) use ($email) {});
        Mail::send('emails.slhemail', ['code' => $code], function($message) use ($email) {
            $message->to($email)->subject('ATW北美商业电讯');
        });
        if(Mail::failures()){
            return ["code" => 0, "msg" => "error"];
        }

        return ["code" => 1, "msg" => "success"];
    }

}



